<?php
require_once _WEB_.'/Protect/Config/define.php';
class ZKF {
	
	/**
	 * 路由處理
	 */
	static public function run(){
		$_uri = preg_replace('/\/+/','/',$_SERVER['REQUEST_URI']);
		$_php = current(explode('?', $_uri));
		include _PUBLIC_ . preg_replace('/\.php?$/','',$_php) . '.php';
	}
	
	/**
	 * 獲取當前時間
	 * @param $status 1=>time(); 2=>date();
	 */
	static public function time($status=1){
		switch ($status){
			case 1:
				return time();
				break;
			case 2:
				return date("Y-m-d H:i:s");
				break;
			default:
				return ;
				break;
		}
	}
	
	/**
	 * 獲取$_GET $_POST參數
	 * @param $key
	 * @param $default
	 * @param $type
	 */
	static public function get($key='',$default='',$type='string') {
		if (empty($key)) {
			$_get = isset($_GET)?$_GET:array();
			$_post = isset($_POST)?$_POST:array();
			return array_merge($_get,$_post);
		}else{
			if (isset($_POST[$key])||isset($_GET[$key])) {
				return isset($_POST[$key])?trim($_POST[$key]):trim($_GET[$key]);
			}else{
				switch ($type){
					case 'int':
						return intval($default);
						break;
					default :
					case 'string' :
						return trim($default);
						break;
				}
			}
		}
	}
		
	/**
	 * 獲取或配置config參數
	 * @param $key 配置KEY
	 * @param $value 配置value，為空時返回配置值，不為空時設置配置的值
	 * @param $conf_file 配置文件名，默認config
	 */
	static public function C($key='',$value='',$conf_file = 'config') {
		static $_config = array();
		if(!isset($_config[$conf_file])){
			$_config[$conf_file] = include(_CONFIG_.'/'.$conf_file.'.php');
		}
		//$conf = array('a'=>111);
		if(array_key_exists($key, $_config[$conf_file])){
			if($value == ''){
				return $_config[$conf_file][$key];
			}else{
				$_config[$conf_file][$key] = $value;
				return true;
			}
		}else{
			return '';
		}
	}
	
	/**
	 * session處理
	 * @param $key
	 * @param $value
	 * @param $status
	 */
	static public function session($key='',$value=null,$status=1) {
		if (!session_id()) session_start();
		if(intval($status)==0 && isset($_SESSION[$key])){
			unset($_SESSION[$key]);
			return ;
		}else{
			if(is_null($value)){
				return isset($_SESSION[$key])?$_SESSION[$key]:null;
			}else{
				$_SESSION[$key] = $value;
				return true;
			}
		}
	}
	
	/**
	 * 優化require_once
	 * @param String $filename	require_once的文件名
	 * @return String
	 */
	static public function require_cache($filename) {
		static $_include_file = array();
		$filename = realpath($filename);
		if (!isset($_include_file[$filename])) {
			if (file_exists($filename)) {
				require $filename;
				$_include_file[$filename] = true;
			} else {
				$_include_file[$filename] = false;
			}
		}
		return $_include_file[$filename];
	}
	
	/**
	 * include lib的class類文件
	 * @param $modelname
	 */
	static public function import($modelname){
		$filename = _LIBRARY_.'/class/'.str_replace('.', '/', $modelname).'.class.php';
		return self::require_cache($filename);
	}
	
	/**
	 * 根據service數組獲取service返回數據
	 * @param unknown_type $param_var 只有一條數據時，返回此條數據array，否則返回所有數據array
	 * @param unknown_type $param_class_action
	 * @param unknown_type $param_get
	 * @param unknown_type $delimiter class與action的分隔符  default '/'
	 * 	1-array		$ret = S($array = array(
						'var_a'=>array('class__action','get','__'),
						'var_b'=>'class__action'
					));
					ZKF::dump($ret['var_a']);
					
		2-string	$ret = S('a','class__action','get');
					ZKF::dump($ret);
		
	 */
	static function S($param_var_class_action='',$param_get='',$delimiter='/'){
		if(!isset($param_var_class_action)) return false;
		$_service_return = array();
		$_var = $_class = $_action = $_gets = '';
		$_isonelist = (is_array($param_var_class_action)&&count($param_var_class_action)>1) ? 0 : 1;
		self::require_cache(_SERVICE_.'/Service.php');
		if(!is_array($param_var_class_action)){
			$param_var_class_action = array(array($param_var_class_action,$param_get,$delimiter));
		}
		foreach ($param_var_class_action as $k=>$v){
			if(!is_array($v)){
				$v = array($v);
			}
			$_delimiter = isset($v[2])?$v[2]:$delimiter;
			$class_action = @explode($_delimiter, $v[0]);
			self::require_cache(_SERVICE_.'/'.$class_action[0].'.php');
			$_class = new $class_action[0]();
			if (method_exists($_class,$class_action[1])) {
				if(isset($v[1])&&$v[1]!=''){
	        		$_service_return[$k] = $_class->$class_action[1]($v[1]);
	        	}else{
	        		$_service_return[$k] = $_class->$class_action[1]();
	        	}
			}else{return false;}
		}
		return $_isonelist==1 ? array_shift($_service_return) : $_service_return;
	}
	
	/**
	 * 獲取model對象
	 * @param String $modelname  對象名稱
	 * @param Array	$param		類參數
	 * @return obj
	 */
	static public function model($modelname, $param = array()) {
	    $modelfile = _MODEL_ . '/' . $modelname . '.php';
	    if (file_exists($modelfile)) {
	    	self::require_cache(_MODEL_.'/Model.php');
	        self::require_cache($modelfile);
	        if (empty($param)) {
	            return new $modelname;
	        } else {
	            return new $modelname($param);
	        }
	    } else {
	        return false;
	    }
	}
	
	/**
	 * 獲取lib class對象
	 * @param String $modelname  對象名稱 例如：ORG.UploadFile
	 * @param Array	 $param      類參數
	 * @return obj
	 */
	static public function lib_class($modelname, $param = array()) {
	    $modelfile = _LIBRARY_ . '/class/' . str_replace('.', '/', $modelname) . '.class.php';
	    if (file_exists($modelfile)) {
	        self::require_cache($modelfile);
	        $arr = explode('.', $modelname);
	        $model = array_pop($arr);
	        if (empty($param)) {
	            return new $model;
	        } else {
	            return new $model($param);
	        }
	    } else {
	        return false;
	    }
	}
	
	/** 
	 *  輸出log信息
	 * @param String $content 提示內容
	 */
	static public function logs($content) {
	    $content = "[" . date("H:i:s") . "] " . $content . "\r\n";
	    file_put_contents(_LOG_.'/'.date("Y-m-d").'.txt', $content, FILE_APPEND);
	}
	
	/**
	 * 創建文件夾
	 * @param $dirstr
	 */
	static public function mkdirs($dirstr = "") {
	    $dirtemp = "";
	    if (empty($dirstr))
	        return false;
	    $dirs = explode("/", $dirstr);
	    $dirtemp.=$dirs[0];
	    for ($i = 1; $i <= count($dirs) + 1; $i++) {
	        if ($dirtemp != "." && $dirtemp != ".." && !empty($dirtemp) && !file_exists($dirtemp)) {
	            if (!@mkdir($dirtemp))
	                return false;
	            chmod($dirtemp, 0777);
	        }
	        if (!empty($dirs[$i]))
	            $dirtemp.="/" . $dirs[$i];
	    }
	    return true;
	}

	/**
	 * 打印輸出array
	 * @param $param
	 */
	static public function dump($param = '') {
		echo '<pre>'.var_export($param,1).'</pre>';
	}
}